# -*- coding: utf-8 -*-
'''
模块：房态维护
输入：cookie,hotel_id,hotel_name
输出：无
'''
import requests
import re
import json
import datetime
import pymysql
import pipelines
from fcs import url_request

from configparser import ConfigParser
cf = ConfigParser()
cf.read("config.ini")


host = cf.get("Mysql-Database", "host")
port = cf.get("Mysql-Database", "port")
user = cf.get("Mysql-Database", "user")
passwd = cf.get("Mysql-Database", "passwd")
db = cf.get("Mysql-Database", "db")
charset = cf.get("Mysql-Database", "charset")
print(host,port,user,passwd,db,charset)
conn = pymysql.connect(host=host, port=int(port), user=user, passwd=passwd, db=db, charset=charset)
cur = conn.cursor()
def main(cookies,ids,name):

    item2 = {}
    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    no = datetime.datetime.now().strftime('%Y%m%d')
    today = datetime.datetime.now().strftime('%Y-%m-%d')
    # 获取酒店名
    # url='http://ebooking.ctrip.com/ebooking/Entry/index2.aspx'
    # con=requests.get(url=url,cookies=cookies).text
    #
    # c=re.compile('<span id="lblHotelName" class="ui-name">(.*?)</span>',re.S)
    # name=re.findall(c,con)
    # if name:
    #     name=name[0]
    #
    # c = re.compile('href="http://hotels.ctrip.com/hotel/(\d*).', re.S)
    # ids = re.findall(c, con)
    # if ids:
    #     ids=ids[0]
    # print(name,ids)

    url='http://ebooking.ctrip.com/ebooking/RoomStatus/ajax/RoomStatusInfo.ashx?v=0.32281092254236343'
    data={
        'Method':'GetRoomTypeNames',
        'CurrentHotel':-1
    }
    hotel_url = 'http://hotels.ctrip.com/hotel/%s.html'%(ids)
    # 储存酒店列表
    sql = "insert ignore into a_ota_back_hotel (hotel_id,hotel_name_cn,create_time,ota_channel_en,update_time,hotel_id_pc,url) values('%s','%s','%s','%s','%s','%s','%s') on duplicate key update update_time='%s'" % (ids, name, now, 'ctrip', now, ids, hotel_url, now)
    cur.execute(sql)
    conn.commit()

    # 获取房型ID
    # con=requests.post(url=url,data=data,cookies=cookies).text
    while True:
        try:
            con=url_request.post_url_requests(url=url,cookies=cookies,headers={},data=data)
            json_data = json.loads(con)
            break
        except:
            continue

    c=re.compile('"RoomTypeId":(.*?),',re.S)
    RoomTypeIds=re.findall(c,con)
    RoomTypeId=[]
    for i in RoomTypeIds:
        RoomTypeId.append(int(i))

    # 根据房型ID获取详细信息
    url='http://ebooking.ctrip.com/ebooking/RoomStatus/ajax/RoomStatusInfo.ashx?v=0.10879674265921757'
    data={
        'Method':'GetHotelRoomStatus',
        'Data':json.dumps({'BeginDate':today,
                'Days':7,
                'RoomTypeIds':RoomTypeId,
                'IsTabGuarantee':1
                }),
        'CurrentHotel':-1
    }

    # con=requests.post(url=url,data=data,cookies=cookies).text

    while True:
        try:
            con=url_request.post_url_requests(url=url,cookies=cookies,headers={},data=data)
            json_data = json.loads(con)
            break
        except:
            continue

    try:
        BasicRoomTypes = json.loads(con)['Data']['BasicRoomTypes']
    except Exception as err:
        print(err)
        return 1

    for BasicRoomType in BasicRoomTypes:
        BasicRoomTypeId = BasicRoomType['BasicRoomTypeId']
        BasicName = BasicRoomType['Name']
        HotelId = BasicRoomType['HotelId']
        HotelName = name
        ota_channel_en = 'ctrip'
        create_time = now
        batch_no = no
        print(HotelId,HotelName,BasicName,BasicRoomTypeId,ota_channel_en,create_time,batch_no)
        # sql = "insert into a_ota_back_hotel_room_type_ctrip (HotelId,HotelName,BasicName,BasicRoomTypeId,ota_channel_en,create_time,batch_no) values ('%s','%s','%s','%s','%s','%s','%s')"%(HotelId,HotelName,BasicName,BasicRoomTypeId,ota_channel_en,create_time,batch_no)
        # cur.execute(sql)
        # conn.commit()
        item1 = {}
        item1.update(table='a_ota_back_hotel_room_type_ctrip')
        item1.update(BasicRoomTypeId=BasicRoomTypeId)
        item1.update(BasicName=BasicName)
        item1.update(HotelId=HotelId)
        item1.update(HotelName=HotelName)
        item1.update(RoomTypeId=RoomTypeId)
        item1.update(ota_channel_en=ota_channel_en)
        item1.update(create_time=create_time)
        item1.update(batch_no=batch_no)
        pipelines.process_item(item1)

        RoomTypes = BasicRoomType['RoomTypes']
        for RoomType in RoomTypes:
            RoomTypeId = RoomType['RoomTypeId']
            RoomName = RoomType['Name']
            PriceType = RoomType['PriceType']
            PriceTypeStatus = RoomType['PriceTypeStatus']
            VendorID = RoomType['VendorID']

            DailyStatus = RoomType['DailyStatus']
            for Daily in DailyStatus:
                Date = Daily['Date']
                DetailStatus = Daily['DetailStatus']
                TotalRooms = Daily['TotalRooms']
                AbleRooms = Daily['AbleRooms']
                ContractRooms = Daily['ContractRooms']
                AbleContractRooms = Daily['AbleContractRooms']
                RoomStatus = Daily['RoomStatus']
                Cost = Daily['Cost']
                Price = Daily['Price']
                PriceShowType = Daily['PriceShowType']

                # print(BasicRoomTypeId, BasicName, HotelId, HotelName, RoomTypeId, RoomName, PriceType, PriceTypeStatus,VendorID, Date, DetailStatus, TotalRooms, AbleRooms, ContractRooms, AbleContractRooms, RoomStatus, Cost, Price, PriceShowType,ota_channel_en,create_time,batch_no)
                item2 = {}
                item2.update(table = 'a_ota_back_hotel_room_type_product_ctrip')
                item2.update(BasicRoomTypeId=BasicRoomTypeId)
                item2.update(BasicName=BasicName)
                item2.update(HotelId=HotelId)
                item2.update(HotelName=HotelName)
                item2.update(RoomTypeId=RoomTypeId)
                item2.update(RoomName=RoomName)
                item2.update(PriceType=PriceType)
                item2.update(PriceTypeStatus=PriceTypeStatus)
                item2.update(VendorID=VendorID)
                item2.update(Date=Date)
                item2.update(DetailStatus=DetailStatus)
                item2.update(TotalRooms=TotalRooms)
                item2.update(AbleRooms=AbleRooms)
                item2.update(ContractRooms=ContractRooms)
                item2.update(AbleContractRooms=AbleContractRooms)
                item2.update(RoomStatus=RoomStatus)
                item2.update(Cost=Cost)
                item2.update(Price=Price)
                item2.update(PriceShowType=PriceShowType)
                item2.update(ota_channel_en=ota_channel_en)
                item2.update(create_time=create_time)
                item2.update(batch_no=batch_no)
                pipelines.process_item(item2)






